package com.shareach.memcache;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

/**
 * 
 * xmemcached实现封装，连接超时默认60秒，操作超时默认1秒
 * @author Shareach
 *
 */
@Service
public class MemcachedService {
	
	@Autowired
	private MemcachedClient memcachedClient;

	// 默认过期时间，2小时
	final static int CACHE_EXPIRY_TIME = 3600 * 2;

	public void put(String key, Object obj, int time) {
		try {
			memcachedClient.set(key, time, obj);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 批量的返回键值对的条目
	 * @param keyList
	 * @return
	 */
	public  Map<String,Object> getBatchedEntry(List<String> keyList){
		Map<String,Object> rt = null;
		try {
			rt = memcachedClient.get(keyList);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return rt;
	}
	

	public void put(String key, Object obj) {
		try {
			memcachedClient.set(key, CACHE_EXPIRY_TIME, obj);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
	}

	public void put(String key, int expireTime ,Object obj) {
		try {
			memcachedClient.set(key, expireTime, obj);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
	}
	
	public boolean exist(String key) {
		return get(key) != null;

	}

	public void replace(String key, Object obj) {
		try {
			memcachedClient.replace(key, CACHE_EXPIRY_TIME, obj);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
	}

	public void remove(String key) {
		try {
			memcachedClient.delete(key);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
	}

	public Object get(String key) {
		Object rt = null;
		try {
			rt = memcachedClient.get(key);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return rt;
	}

	public void clear() {
		try {
			memcachedClient.flushAll();
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
	}
}
